<?php

namespace app\admin\controller;
use think\Controller;
/**
 * Created by PhpStorm.
 * User: times
 * Date: 2018/5/11
 * Time: 08:39
 */
//团购商品列表
class Featured extends Controller
{
    private  $obj;
    public function _initialize() {
        $this->obj = model("Featured");
    }

    public function add() {
        if(request()->isPost()) {
            // 入库的逻辑
            $data = input('post.');
            //图片处理
            $info0 = "";
            $logo = request()->file('logo');
            if ($logo) {
                $info0 = $logo->rule("uniqid")->validate(["size" => 3000000, "ext" => "jpg,png,gif"])->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'Simages' . DS . 'tuijianwei');
            }
            $data['image'] =$info0->getFilename();
            $datas = [
                'title' => $data['title'],
                'type' => $data['type'],
                'url' => $data['url'],
                'description' => $data['description'],
                'image' => $data['image'],
            ];
            $id = model('Featured')->add($datas);
            if($id) {
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else {
            // 获取推荐位类别
            $types = config('featured.featured_type');
            return $this->fetch('', [
                'types' => $types,
            ]);
        }
    }
    public function index() {
        // 获取推荐位类别
        $types = config('featured.featured_type');
        $type = input('get.type', 0 ,'intval');
        // 获取列表数据
        $results = $this->obj->getFeaturedsByType($type);

        return $this->fetch('', [
            'types' => $types,
            'results' => $results,

        ]);
    }

    public function change(){
        $data = input('get.');
        $res = $this->obj->save(['status'=>$data['status']], ['id'=>$data['id']]);
        if($res) {
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }

}
